// pages/login/atLogin/atLogin.js
const app = getApp()
var jumpToAibo = false;
Page({
  /**
   * 页面的初始数据
   */
  data: {
    mobile:'',
    jsCode:'',
    msg:"",
    code:'',
    showInviteCodeInput: false,
    textInputValue: "", // Store the value of the text input field
    ischeck:false,
    // inviteCode:''
  },


  // Handle checkbox change event
  onCheckboxChange(event) {
    console.log(event)
    var isChecked = !this.data.ischeck
    this.data.ischeck = isChecked
     console.log(isChecked)
     if(isChecked == false){
      this.setData({
        textInputValue: '',
      });
     }
    this.setData({
      // showInviteCodeInput: isChecked,
      ischeck:isChecked
    });
  },

  // Handle text input change event
  onInput(event) {
    const inputValue = event.detail.value;
    this.setData({
      textInputValue: inputValue,
    });
  },
  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    console.log(options)
    if(options.aiboType!=undefined){
      jumpToAibo = true
    }
  },
  wxLogin:function(){
    var that = this;    
    if(this.data.ischeck){
      if(this.data.textInputValue==''){
        wx.showToast({
          title: '请输入邀请码',
          icon:'none'
        })
        return
      }
    }
    wx.login({
      success: res => {
        console.log(res)
        // wx.hideLoading()
        // that.setData({
        //   code:res.code 
        // })
        // return
        // 发送 res.code 到后台换取 openId, sessionKey, unionId
        this.data.jsCode = res.code 
        console.log(res.code)   
 
        wx.request({
          url: app.app_url.BASE_URL+app.app_url.LOGIN ,
          header:{
            'content-type': 'application/json',
          },
          method:'POST',
          data:{
            "wxLogonJSCode":res.code,
            'inviteCode':that.data.textInputValue
          },success(res){
            wx.hideLoading()
            console.log(res)
            if(res.statusCode ==501){
              console.log(res)   
              wx.showToast({
                title: res.data,
                icon:'error'
              })
              return
            }
            if(res.data.errcode == 0){
              var loginData = {
                OpenID:res.data.data.openid
              }
              app.loginSuccess(loginData)
              if(jumpToAibo){
                wx.navigateBack()
              }else{
                wx.switchTab({
                  url: '/pages/sita/prompts/prompts',
                })
            }
            }else{
              wx.hideLoading()
              wx.showToast({
                title: '登录失败',
                icon:'error'
              })
            }       
          },fail(res){
         
            wx.hideLoading()
            wx.showToast({
              title: '登录失败',
              icon:'error'
            })
          }
        })
      },
      fail(res){
        wx.hideLoading()
        that.loginError();
        
      }
    })
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {

  },
  /**
   * 获取登录手机号
   */
  input_phone:function(res){
    
    this.data.mobile = res.detail.value;
  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {

  },

  loginError(res){
    wx.hideLoading();
    wx.showToast({
      title:'登录失败',
      icon: 'none'
    })
  },
  loginAPP(res){
    wx.showLoading({
      title: '请求中',
    })
     this.wxLogin();
  
  },
 

 
  



})